package com.ltmonitor.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.Terminal;
import com.ltmonitor.service.ITerminalService;
import com.ltmonitor.web.util.JsonMessage;

/**
 * 终端信息
 * 
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/terminalInfo")
public class TerminalInfoAction  extends PaginateAction {
	

	@Autowired
	private ITerminalService terminalService;

	@RequestMapping("/viewList.action")
	public String list()
	{
		return "/terminal/terminalList";
	}
	@RequestMapping("/view.action")
	public String view(Model m,
			@RequestParam(value = "entityId", required = false, defaultValue="0") long id) {
		try {
			if (id > 0) {
				Terminal t = this.terminalService.load(id);
				m.addAttribute("entity", t);
			}else
			{
				Map entity = new HashMap();
				entity.put("entityId", 0);
				m.addAttribute("entity", entity);				
			}
			//m.addObject("message", "保存成功");

		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addAttribute("message","发生错误:"+ex.getMessage());
		}
		return "/terminal/editTerminal";		
	}

	@RequestMapping("/delete.action")
	@ResponseBody
	public JsonMessage delete(long entityId) {
		try {
			this.terminalService.deleteFakeById(entityId);
			return json(true,"删除成功");
		} catch (Exception e) {
			return json(false, e.getMessage());
		}
	}
	

	/**
	 * 新增后的保存操作
	 * 
	 * @return
	 */
	@RequestMapping("/save.action")
	public String save(Terminal t, Model m) throws Exception {

		try {
			t.setCompanyId(this.getOnlineUser().getCompanyId());
			this.terminalService.saveOrUpdate(t);

			m.addAttribute("message","保存成功");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			m.addAttribute("message",ex.getMessage());
		}

		m.addAttribute("entity", t);
		return "/terminal/editTerminal";		
	}


}
